$(function () {

    //关键词列表列表数据
    function getTable() {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Keyword/GetList?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Keyword/GetList?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                'token': _token
            },
            data: JSON.stringify({
                "checkCode": 0,
            }),
            xhrFields: {withCredentials: true},
            success: function (data) {
                console.log(data);
                var result = JSON.stringify(data);
                if (data.code == 1) {
                    $("#KeyWordTable tbody tr").remove();
                    var count = data.data.length;
                    for (var i = 0; i < count; i++) {
                        var _id = data.data[i].id || '';
                        var _info = data.data[i].info || '';
                        var _keyword = data.data[i].keyword || '';
                        var _type = data.data[i].type || '';
                        var tr = "<tr _id=" + _id + " _type=" + _type + " _keyword="+_keyword+" _info="+_info+"><td>" + _id + "</td><td>" + _keyword + "</td><td>" + _info + "</td><td>" + _type + "</td><td><button class='deleteKey'><i class='icon-trash'>&nbsp;</i>删除</button></td></tr>";
                        $("#KeyWordTable tbody").append(tr);
                    }
                    deleteKeyWord(".deleteKey");
                }
            },
            error: function () {
                alert('请求失败');
            }
        });
    }

    getTable();

    //表头固定
    var tableCont = document.querySelector('#KeyWordTable');
    // tableCont.addEventListener('scroll',scrollHandle);

    // 删除关键字
    function deleteKeyWord(idClass) {
        $(idClass).click(function () {
            var that = $(this);
            var _id = $(this).parent().parent().attr("_id");
            var _type = $(this).parent().parent().attr("_type");
            var _info = $(this).parent().parent().attr("_info");
            var _keyword = $(this).parent().parent().attr("_keyword");

            var _timestamp = new Date().getTime();
            var _secret = hex_md5(ip + '/Back/Keyword/Del?timestamp=' + _timestamp + salt);
            $.ajax({
                type: "POST",
                url: ip + '/Back/Keyword/Del?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
                dataType: 'json',
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json;charset=utf-8",
                    'token': _token
                },
                data: JSON.stringify({
                    "id": _id,
                    "info": _info,
                    "keyword": _keyword,
                    "type": _type
                }),
                xhrFields: {withCredentials: true},
                success: function (data) {
                    console.log(data);
                    if (data.code == 1) {
                        alert(data.message);
                        that.parent().parent().fadeOut();
                    }
                },
                error: function () {
                    alert('请求失败');
                }
            });
        })
    }


    //查找关键字
    $("#queryKeyWord").click(function () {
        var keyWord = $("#KeyIput").val();
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Keyword/Search?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Keyword/Search?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                'token': _token
            },
            data: JSON.stringify({
                "key": keyWord
            }),
            xhrFields: {withCredentials: true},
            success: function (data) {
                console.log(data);
                if (data.code == 1) {
                    var count = data.data.length;
                    $("#KeyWordTable tbody").remove();
                    for(var i=0;i<count;i++){
                        var _id = data.data[i].id || '';
                        var _info = data.data[i].info || '';
                        var _keyword = data.data[i].keyword || '';
                        var _type = data.data[i].type || '';
                        var tr = "<tr _id=" + _id + " _type=" + _type + "><td>" + _id + "</td><td>" + _keyword + "</td><td>" + _info + "</td><td>" + _type + "</td><td><button class='queryDelete'>删除</button></td></tr>";
                        $("#KeyWordTable").append(tr)
                    }
                    deleteKeyWord(".queryDelete");
                    $("#CzView").fadeIn();
                }
                else {
                    alert(data.message)
                }

            },
            error: function () {
                alert('请求失败');
            }
        });
    });
    //关闭查找表
    $(".closeCzView").click(function () {
       $("#CzView").fadeOut();
    });


    // 新增关键字
    $("#addKeyWordNow").click(function () {
        var _info = $("#addNewInfo").val();
        var _keyword = $("#addNewKeyword").val();
        var _type = $("#addNewType").val();
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Keyword/Insert?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Keyword/Insert?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                'token': _token
            },
            data: JSON.stringify({
                "info": _info,
                "keyword": _keyword,
                "type": _type
            }),
            xhrFields: {withCredentials: true},
            success: function (data) {
                console.log(data);
                if (data.code == 1) {
                    alert(data.message);
                    getTable();
                }
                else {
                    alert(data.message);

                }
            },
            error: function () {
                alert('请求失败');
            }
        });
    });

    $("#closeAddKeyView").click(function () {
        $(".add_div").fadeOut();
    });
    $("#addKeyWordView").click(function () {
        $(".add_div").fadeToggle();
    })
});